package ar.fiuba.fallar2.pac_man.pregunta.persist;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ar.fiuba.fallar2.pac_man.AbstractDataMapper;
import ar.fiuba.fallar2.pac_man.DBManager;
import ar.fiuba.fallar2.pac_man.IBusinessObject;
import ar.fiuba.fallar2.pac_man.pregunta.model.Pregunta;

public class PreguntaDataMapper extends AbstractDataMapper{

	@Override
	public List findAll() 
	{
		try {
			List<IBusinessObject> lista = new ArrayList<IBusinessObject>();
			String sqlQuery = "SELECT * FROM "+getTable();
			ResultSet r = DBManager.getInstance().find(sqlQuery).getResultSet();
			while (r.next()) 
				lista.add(createBO(r));
			
			r.getStatement().close();
			return lista;
		 } catch (SQLException e) {
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBusinessObject load(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	

@Override
protected IBusinessObject createBO(ResultSet r) throws  SQLException {
	
	 Pregunta pregunta= new Pregunta(r.getInt("PR_id"),
			 				r.getInt("PR_ExistenteId"),
			 				r.getString("PR_Pregunta"),
			 			    r.getInt("PR_SINO"));
	 
	return  pregunta;
}

@Override
protected String getIdColumn() {
	// TODO Auto-generated method stub
	return "PR_id";
}

@Override
protected String getTable() {
	// TODO Auto-generated method stub
	return "Pregunta";
}

@Override
protected String mapBOInsert(IBusinessObject r) throws SQLException {
	Pregunta pregunta = (Pregunta) r;
	StringBuffer sb = new StringBuffer();
	sb.append("INSERT INTO " + getTable());
	sb.append("( PR_ExistenteId ,PR_Pregunta,PR_SINO ) VALUES (");
	sb.append(pregunta.getIdExistente()+", '");
	sb.append(pregunta.getPreguntaSINO()+", '");
	
	sb.append(pregunta.getSiNo()+"') ");
	return sb.toString();
}

@Override
protected String mapBOUpdate(IBusinessObject r) throws SQLException {

	Pregunta pregunta   = (Pregunta) r;
	StringBuffer sb = new StringBuffer();
	sb.append("UPDATE " + getTable());
	sb.append("  PR_ExistenteId='"+pregunta.getIdExistente());
	sb.append(", PR_Pregunta='"+pregunta.getPreguntaSINO());
	sb.append(", PR_SINO='"+pregunta.getSiNo());
	sb.append("' WHERE PR_Id="+pregunta.getId()+")");
	return sb.toString();
}
	


}


